package com.atguigu.guli.service.oss.controller;

import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.oss.service.FileService;
import com.google.gson.Gson;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.concurrent.TimeUnit;

@RestController

@RequestMapping("/admin/oss/file")
public class FileController {
    @Autowired
    FileService fileService;

//    @GetMapping("test")
//    public R test(String username,String password){
//        System.out.println("oss微服务被调用了。。。。");
//        System.out.println("username = " +username);
//        System.out.println("password = " + password);
//        return R.ok().message("filecontroller的响应");
//    }

    @Value("${server.port}")
    Integer port;

    @PostMapping("test")
    public R test(@RequestBody ResultCodeEnum resultCodeEnum) throws InterruptedException {
        //休眠3秒
    TimeUnit.MILLISECONDS.sleep(3000);


        System.out.println("端口号为："+port+"oss微服务被调用了。。。。");
        System.out.println("resultCodeEnum = " + resultCodeEnum.getMessage() + "," + resultCodeEnum.getCode() + "," + resultCodeEnum.getSuccess());
        return R.ok().message("filecontroller的响应");
    }


    //1.处理文件上传，返回文件地址

    @PostMapping("upload")
    public R uploadFile(MultipartFile file, @RequestParam("module") String module) {//module上传的文件要保存到桶内的目录

        String path = fileService.uploadFile(file, module);

        return R.ok().data("path", path);
    }


    //2.删除文件
    @DeleteMapping("delete")
    //path：文件路径  module:文件在桶内的目录名称
    public R deleteFile(@RequestParam("path") String path,
                        @RequestParam("module") String module) {
        fileService.deleteFile(path, module);

        return R.ok();
    }


}
